package com.tbm.console.proj.controller;

import com.alibaba.fastjson.JSONArray;
import com.tbm.console.base.controller.BaseController;
import com.tbm.console.common.constant.ConsoleConstant;
import com.tbm.console.common.constant.InsUpdDelFlagConstant;
import com.tbm.console.proj.entity.ColumnEntity;
import com.tbm.console.proj.entity.EquipmentEntity;
import com.tbm.console.proj.service.ColumnService;
import com.tbm.console.proj.service.EquipmentService;
import com.tbm.console.util.Array2List;
import com.tbm.console.util.EntityAttributeUtil;
import com.tbm.console.util.JurisdictionUtil;
import com.tbm.console.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Controller
public class EquipmentController extends BaseController {

    @Autowired
    private EquipmentService equipmentService;

    @Autowired
    private ColumnService columnService;

    /**
     * 页面展示
     * @return
     */
    @RequestMapping("/equipment-list")
    public ModelAndView equipmentList(){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            Object attribute = this.getRequest().getSession().getAttribute(ConsoleConstant.BTNRIGHTS);
            List<PageData> buttonList = (List<PageData>) JSONArray.parse(attribute.toString());
            modelAndView.addObject("buttons", buttonList);

            PageData pd = new PageData();
            pd.put("className", "EquipmentEntity");
            pd.put("deleteFlag", InsUpdDelFlagConstant.DELETE_FLAG_N);
            List<ColumnEntity> all = columnService.findAll(pd);

            List<PageData> attributes = new ArrayList<>();
            List<PageData> freezAttributes = new ArrayList<>();
            if(all.size()==1){
                ColumnEntity columnEntity = all.get(0);
                List<String> allList = Array2List.transferArrayToList(columnEntity.getAllName().trim().split(" "));
                List<String> freezeList = Array2List.transferArrayToList(columnEntity.getFreezeName().trim().split(" "));
                List<String> hiddenList = Array2List.transferArrayToList(columnEntity.getHiddenName().trim().split(" "));
                //冻结的去掉隐藏的
                freezeList.removeAll(hiddenList);
                allList.removeAll(hiddenList);
                allList.removeAll(freezeList);
                attributes = EntityAttributeUtil.entityAttributeWithZH(EquipmentEntity.class, allList);
                freezAttributes = EntityAttributeUtil.entityAttributeWithZH(EquipmentEntity.class, freezeList);
                modelAndView.addObject("freeze", freezAttributes);
            }else {
                attributes = EntityAttributeUtil.entityAttributeWithZH(EquipmentEntity.class);
            }

            //应该在这里添加列头
            modelAndView.addObject("attributes", attributes);
            viewName = "equipment/equipment-list";
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }

    @RequestMapping("/equipment-add/{id}")
    public ModelAndView equipmentAdd(@PathVariable("id") String id){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            EquipmentEntity equipmentEntity = equipmentService.findById(Integer.valueOf(id));
            modelAndView.addObject("entity", equipmentEntity);
            viewName = "equipment/equipment-add";
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }
}
